package com.gigi.service.manual;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gigi.entity.TagShiqinEntity;
import com.gigi.enums.ErrorCodeEnum;
import com.gigi.enums.YesOrNoEnum;
import com.gigi.mapper.generated.TagShiqinMapper;
import com.gigi.model.ListRequest;
import com.gigi.model.TypeTagDO;
import com.gigi.service.generated.TagShiqinService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class TagManageService {

    private final TagShiqinService tagShiqinService;
    private final TagShiqinMapper tagShiqinMapper;

    public TagManageService(TagShiqinService tagShiqinService, TagShiqinMapper tagShiqinMapper) {
        this.tagShiqinService = tagShiqinService;
        this.tagShiqinMapper = tagShiqinMapper;
    }

    /**
     * 列表接口
     *
     * @param request
     * @return
     */
    public MultiResponse<TagShiqinEntity> list(ListRequest request) {
        if (request.getPageNum() < 1) {
            request.setPageNum(1);
        }
        if (request.getPageSize() < 1) {
            request.setPageSize(10);
        }

        List<TagShiqinEntity> list;
        int total = 0;

        if (YesOrNoEnum.YES.getCode().equals(request.getIfByPage())) {
            Page<TagShiqinEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
            Page<TagShiqinEntity> tagPage = tagShiqinMapper.selectPage(page, null);
            list = tagPage.getRecords();
            total = (int) tagPage.getTotal();

        } else {
            list = tagShiqinService.list();
        }
        return MultiResponse.of(list, total);
    }

    /**
     * 详情接口
     *
     * @param id
     * @return
     */
    public SingleResponse<TagShiqinEntity> detail(Long id) {
        TagShiqinEntity entity = tagShiqinMapper.selectById(id);
        if (Objects.isNull(entity)) {
            return SingleResponse.buildFailure(ErrorCodeEnum.NO_DATA.getErrCode(), ErrorCodeEnum.NO_DATA.getErrDesc());
        }
        return SingleResponse.of(entity);
    }

    /**
     * 保存接口
     *
     * @param request
     * @return
     */
    public Response save(TypeTagDO request) {
        if (ObjectUtil.isNull(request.getId())) {
            TagShiqinEntity target = new TagShiqinEntity();
            target.setName(request.getName());
            if (!tagShiqinService.save(target)) {
                return Response.buildFailure(ErrorCodeEnum.SAVE_FAILED.getErrCode(), ErrorCodeEnum.SAVE_FAILED.getErrDesc());
            }
        } else {
            TagShiqinEntity target = new TagShiqinEntity();
            target.setName(request.getName());
            target.setId(request.getId());
            if (!tagShiqinService.updateById(target)) {
                return Response.buildFailure(ErrorCodeEnum.EDIT_FAILED.getErrCode(), ErrorCodeEnum.EDIT_FAILED.getErrDesc());
            }
        }
        return Response.buildSuccess();
    }

    /**
     * 删除接口
     *
     * @param id
     * @return
     */
    public Response delete(Long id) {
        if (!tagShiqinService.removeById(id)) {
            return Response.buildFailure(ErrorCodeEnum.DELETE_FAILED.getErrCode(), ErrorCodeEnum.DELETE_FAILED.getErrDesc());
        }
        return Response.buildSuccess();
    }

}
